-- 30.lua
-- Created by zouyb Jun/26/2015
-- 角斗士BUFF光效

return {
    onAppear = function(monster)    
        -- 当前怪物的基类ID
        local baseClassId = monster:query("base_class_id");

        -- 获取其他所有怪物
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                count = count + 1;
            end          
        end

        if #openMonsterGrids == 1 then
            -- 只有角斗士一只怪时，播放“属性增益”光效
            local monsterGrid = UIDungeonMgr:getCurLevel().grids[monster:getPos()];
            playAddBuffEffect(monsterGrid, BUFF_TYPE_ATTACK);
            monsterGrid.hasPlayedBuff = true;
        end

        -- 注册其他怪物死亡时的回调
        if count > 0 then
            UIDungeonMgr:getCurLevel().monsterBuffEffectM:addEventModule("monster30_appear");
            EventMgr.removeAll("monster30_appear");
            EventMgr.register("monster30_appear", event.MONSTER_DIE_LOGIC, function(params)
                local pos = params.pos;
                local grid = DungeonM.getGridByPos(pos);
                if grid.monster then
                    local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
                    local leftMonsterGrid;
                    local leftCount = 0;
                    for i = 1, #openMonsterGrids do
                        local monsterGrid = openMonsterGrids[i];
                        if monsterGrid.gridData.monster ~= grid.monster and monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                            leftMonsterGrid = monsterGrid;
                            leftCount = leftCount + 1;
                        end          
                    end
                                                 
                    -- 剩下两只怪，其中角斗士活着，另一只怪已死亡，角斗士播放"属性增益"光效                    
                    if #openMonsterGrids == 1 and leftCount == 1 then
                        playAddBuffEffect(leftMonsterGrid, BUFF_TYPE_ATTACK);
                        leftMonsterGrid.hasPlayedBuff = true;
                    end
                end
            end);

            local function onMonsterAppear()
                local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
                -- 角斗士存在属性增益时，翻出其他怪物，该角斗士播放“属性减益” 光效
                if #openMonsterGrids > 1 then
                    for i = 1, #openMonsterGrids do
                        local monsterGrid = openMonsterGrids[i];
                        if monsterGrid.hasPlayedBuff then
                            playAddDeBuffEffect(monsterGrid, DEBUFF_TYPE_ATTACK);
                            monsterGrid.hasPlayedBuff = false;
                        end          
                    end
                end
            end

            -- 翻出怪物的回调处理
            EventMgr.register("monster30_appear", event.UI_GRID_OPEN, function(params)
                local pos = params.pos;
                local grid = DungeonM.getGridByPos(pos);
                if grid.monster then            
                    onMonsterAppear();
                end
            end);

            -- 暂不考虑直接刷怪的情况
        end        
    end,

    onDie = function(monster)    
        -- 当前怪物的基类ID
        local baseClassId = monster:query("base_class_id");

        -- 获取其他所有野猪
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                count = count + 1;
            end
        end   

        if count == 0 then  
            EventMgr.removeAll("monster30_appear");
        end
    end,
};